# -*- coding: utf-8 -*-
#内建模块---datetime 处理日期和时间的标准库

from datetime import datetime,timedelta,timezone

now = datetime.now()

print(now)
print(type(now))

#指定日期
dt = datetime(2014,4,19,12,20)
print(dt)

# 将datetime转换为timestamp
print(dt.timestamp())

#本地时间
print(datetime.fromtimestamp(dt.timestamp()))

#UTC时间
print(datetime.utcfromtimestamp(dt.timestamp()))


# str 转换为datetime
cday = datetime.strptime('2015-6-1 18:19:59','%Y-%m-%d %H:%M:%S')
print(cday)

# datetime 转换为str
print(datetime.now().strftime('%a, %b %d %H:%M'))

# datetime 加减
now - datetime.now()
print(now)
print(now + timedelta(hours=10))
print(now - timedelta(days=1))
print(now + timedelta(days=2,hours=12))

# 本地时间转换为UTC时间
# 创建时区 UTC+8:00
tc_utc_8 = timezone(timedelta(hours=8))
now = datetime.now()
print(now)
# 强制设置为UTC+8:00
dt = now.replace(tzinfo = tc_utc_8)
print(dt)

# 时区转换
utc_dt = datetime.utcnow().replace(tzinfo=timezone.utc)
print(utc_dt)
# astimezone() 将转换时区为背景时间
bj_dt = utc_dt.astimezone(timezone(timedelta(hours=8)))
print(bj_dt)